package com.minesweeper.dialog;

import com.minesweeper.data.GameData;
import com.minesweeper.ui.GameInterface;

import javax.swing.*;

/**
 * @author skniy
 */
public class UserCustom extends JDialog {

    /**
     * 游戏界面
     */
    private GameInterface gameInterface;

    /**
     * 构造方法
     *
     * @param gameInterface
     */
    public UserCustom(GameInterface gameInterface) {
        //调用父类构造方法并初始化
        super(gameInterface);
        this.gameInterface = gameInterface;
        //加入组件
        addItems();
        //为了测试,不设置模态
        if (gameInterface!=null) {
            this.setModal(true);
        }
        //设置自身属性
        this.setTitle("自定义游戏");
        this.setSize(210, 150);
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setVisible(true);
    }

    /**
     * 加入组件
     */
    private void addItems() {
        //设置布局
        this.setLayout(null);
        //加入行数
        JLabel jLabelRows = new JLabel("行数：");
        jLabelRows.setBounds(20, 20, 40, 20);
        JTextField jTextFieldRows = new JTextField(String.valueOf(GameData.ROWS));
        jTextFieldRows.setBounds(60, 20, 30, 20);
        this.add(jLabelRows);
        this.add(jTextFieldRows);
        //加入列数
        JLabel jLabelCols = new JLabel("列数：");
        jLabelCols.setBounds(20, 50, 40, 20);
        JTextField jTextFieldCols = new JTextField(String.valueOf(GameData.COLS));
        jTextFieldCols.setBounds(60, 50, 30, 20);
        this.add(jLabelCols);
        this.add(jTextFieldCols);
        //加入地雷数目
        JLabel jLabelMines = new JLabel("雷数：");
        jLabelMines.setBounds(20, 80, 40, 20);
        JTextField jTextFieldMines = new JTextField(String.valueOf(GameData.MINE_COUNT));
        jTextFieldMines.setBounds(60, 80, 30, 20);
        this.add(jLabelMines);
        this.add(jTextFieldMines);
        //加入确定按钮
        JButton jButtonOk = new JButton("确定");
        jButtonOk.setBounds(120, 20, 60, 25);
        jButtonOk.addActionListener(e -> {
            String result = checkInput(jTextFieldRows.getText(), jTextFieldCols.getText(), jTextFieldMines.getText());
            //只有用户输入正确时
            if ("".equals(result)) {
                GameData.ROWS = Integer.parseInt(jTextFieldRows.getText());
                GameData.COLS = Integer.parseInt(jTextFieldCols.getText());
                GameData.MINE_COUNT = Integer.parseInt(jTextFieldMines.getText());
                gameInterface.restartGame();
                UserCustom.this.dispose();
            }//用户输入不正确时
            else {
                JOptionPane.showMessageDialog(null, result, "输入信息错误", JOptionPane.ERROR_MESSAGE);
            }
        });
        this.add(jButtonOk);
        //加入取消按钮
        JButton jButtonCancel = new JButton("取消");
        jButtonCancel.setBounds(120, 70, 60, 25);
        jButtonCancel.addActionListener(e -> UserCustom.this.dispose());
        this.add(jButtonCancel);
    }

    /**
     * 检查输入
     *
     * @param rows
     * @param cols
     * @param mineCount
     * @return
     */
    private String checkInput(String rows, String cols, String mineCount) {
        String result = "";
        //检查输入是否是数字
        String number = "-?[1-9][0-9]*|0";
        int min = 9;
        int max = 30;
        if (!rows.matches(number)) {
            result = "行数应输入数字！";
        } else if (!cols.matches(number)) {
            result = "列数应输入数字！";
        } else if (!mineCount.matches(number)) {
            result = "地雷数应输入数字！";
        }//检查输入的范围
        else if (Integer.parseInt(rows) < min || Integer.parseInt(rows) > max) {
            result = "行数应在9~30之间！";
        } else if (Integer.parseInt(cols) < min || Integer.parseInt(cols) > max) {
            result = "列数应在9~30之间！";
        }//检查地雷数目是否正确
        else if (Integer.parseInt(mineCount) < 1) {
            result = "地雷数目不能小于1！";
        } else if (Integer.parseInt(mineCount) >= Integer.parseInt(rows) * Integer.parseInt(cols)) {
            result = "地雷数目不能超过或等于格子数目！";
        }

        return result;
    }

}
